﻿#region 头注释
/******************************************************************************
文件名称    :   ChemistryBiochemistry.cs
创建日期    :   2011/5/25 22:20:46
功能描述    :   化验血生实体类
修改日志    :   
    2011-05-25 22:20  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// 化验血生实体类
    /// </summary>
    public class ChemistryBiochemistry : EntityBase<ChemistryBiochemistry, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ChemistryBiochemistry()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            urea = null;
            na = null;
            ka = null;
            cl = null;
            acid = null;
            ca = null;
            sugar = null;
            creatinine = null;
            co2 = null;
            uricAcid = null;
            citricAcid = null;
            phosphorus = null;
            albumin = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override ChemistryBiochemistry CreateNew()
        {
            return new ChemistryBiochemistry();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.ChemistryBiochemistry; }
        }

        /// <summary>
        /// 获取或设置尿素
        /// </summary>
        public decimal? Urea
        {
            get { return urea; }
            set
            {
                if (entityState == EntityState.Saved && urea != value) entityState = EntityState.Changed;
                urea = value;
            }
        }
        protected decimal? urea;

        /// <summary>
        /// 获取或设置钠
        /// </summary>
        public decimal? Na
        {
            get { return na; }
            set
            {
                if (entityState == EntityState.Saved && na != value) entityState = EntityState.Changed;
                na = value;
            }
        }
        protected decimal? na;

        /// <summary>
        /// 获取或设置钾
        /// </summary>
        public decimal? Ka
        {
            get { return ka; }
            set
            {
                if (entityState == EntityState.Saved && ka != value) entityState = EntityState.Changed;
                ka = value;
            }
        }
        protected decimal? ka;

        /// <summary>
        /// 获取或设置氯
        /// </summary>
        public decimal? Cl
        {
            get { return cl; }
            set
            {
                if (entityState == EntityState.Saved && cl != value) entityState = EntityState.Changed;
                cl = value;
            }
        }
        protected decimal? cl;

        /// <summary>
        /// 获取或设置草酸
        /// </summary>
        public decimal? Acid
        {
            get { return acid; }
            set
            {
                if (entityState == EntityState.Saved && acid != value) entityState = EntityState.Changed;
                acid = value;
            }
        }
        protected decimal? acid;

        /// <summary>
        /// 获取或设置钙
        /// </summary>
        public decimal? Ca
        {
            get { return ca; }
            set
            {
                if (entityState == EntityState.Saved && ca != value) entityState = EntityState.Changed;
                ca = value;
            }
        }
        protected decimal? ca;

        /// <summary>
        /// 获取或设置葡萄糖
        /// </summary>
        public decimal? Sugar
        {
            get { return sugar; }
            set
            {
                if (entityState == EntityState.Saved && sugar != value) entityState = EntityState.Changed;
                sugar = value;
            }
        }
        protected decimal? sugar;

        /// <summary>
        /// 获取或设置肌酐
        /// </summary>
        public decimal? Creatinine
        {
            get { return creatinine; }
            set
            {
                if (entityState == EntityState.Saved && creatinine != value) entityState = EntityState.Changed;
                creatinine = value;
            }
        }
        protected decimal? creatinine;

        /// <summary>
        /// 获取或设置二氧化碳结合力
        /// </summary>
        public decimal? Co2
        {
            get { return co2; }
            set
            {
                if (entityState == EntityState.Saved && co2 != value) entityState = EntityState.Changed;
                co2 = value;
            }
        }
        protected decimal? co2;

        /// <summary>
        /// 获取或设置尿酸
        /// </summary>
        public decimal? UricAcid
        {
            get { return uricAcid; }
            set
            {
                if (entityState == EntityState.Saved && uricAcid != value) entityState = EntityState.Changed;
                uricAcid = value;
            }
        }
        protected decimal? uricAcid;

        /// <summary>
        /// 获取或设置枸橼酸
        /// </summary>
        public decimal? CitricAcid
        {
            get { return citricAcid; }
            set
            {
                if (entityState == EntityState.Saved && citricAcid != value) entityState = EntityState.Changed;
                citricAcid = value;
            }
        }
        protected decimal? citricAcid;

        /// <summary>
        /// 获取或设置磷
        /// </summary>
        public decimal? Phosphorus
        {
            get { return phosphorus; }
            set
            {
                if (entityState == EntityState.Saved && phosphorus != value) entityState = EntityState.Changed;
                phosphorus = value;
            }
        }
        protected decimal? phosphorus;

        /// <summary>
        /// 获取或设置白蛋白
        /// </summary>
        public decimal? Albumin
        {
            get { return albumin; }
            set
            {
                if (entityState == EntityState.Saved && albumin != value) entityState = EntityState.Changed;
                albumin = value;
            }
        }
        protected decimal? albumin;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override ChemistryBiochemistry CopyTo(ChemistryBiochemistry entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.urea = this.urea;
            entity.na = this.na;
            entity.ka = this.ka;
            entity.cl = this.cl;
            entity.acid = this.acid;
            entity.ca = this.ca;
            entity.sugar = this.sugar;
            entity.creatinine = this.creatinine;
            entity.co2 = this.co2;
            entity.uricAcid = this.uricAcid;
            entity.citricAcid = this.citricAcid;
            entity.phosphorus = this.phosphorus;
            entity.albumin = this.albumin;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<ChemistryBiochemistry>
        {
            ChemistryBiochemistry entity;
            public DefaultXmlSerializer(ChemistryBiochemistry entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public ChemistryBiochemistry Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Urea":
                        entity.urea = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Na":
                        entity.na = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Ka":
                        entity.ka = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Cl":
                        entity.cl = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Acid":
                        entity.acid = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Ca":
                        entity.ca = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Sugar":
                        entity.sugar = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Creatinine":
                        entity.creatinine = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Co2":
                        entity.co2 = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "UricAcid":
                        entity.uricAcid = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "CitricAcid":
                        entity.citricAcid = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Phosphorus":
                        entity.phosphorus = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Albumin":
                        entity.albumin = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.Urea != null) writer.WriteElementString("Urea", XmlConvert.ToString(entity.Urea.Value));
                if (entity.Na != null) writer.WriteElementString("Na", XmlConvert.ToString(entity.Na.Value));
                if (entity.Ka != null) writer.WriteElementString("Ka", XmlConvert.ToString(entity.Ka.Value));
                if (entity.Cl != null) writer.WriteElementString("Cl", XmlConvert.ToString(entity.Cl.Value));
                if (entity.Acid != null) writer.WriteElementString("Acid", XmlConvert.ToString(entity.Acid.Value));
                if (entity.Ca != null) writer.WriteElementString("Ca", XmlConvert.ToString(entity.Ca.Value));
                if (entity.Sugar != null) writer.WriteElementString("Sugar", XmlConvert.ToString(entity.Sugar.Value));
                if (entity.Creatinine != null) writer.WriteElementString("Creatinine", XmlConvert.ToString(entity.Creatinine.Value));
                if (entity.Co2 != null) writer.WriteElementString("Co2", XmlConvert.ToString(entity.Co2.Value));
                if (entity.UricAcid != null) writer.WriteElementString("UricAcid", XmlConvert.ToString(entity.UricAcid.Value));
                if (entity.CitricAcid != null) writer.WriteElementString("CitricAcid", XmlConvert.ToString(entity.CitricAcid.Value));
                if (entity.Phosphorus != null) writer.WriteElementString("Phosphorus", XmlConvert.ToString(entity.Phosphorus.Value));
                if (entity.Albumin != null) writer.WriteElementString("Albumin", XmlConvert.ToString(entity.Albumin.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<ChemistryBiochemistry>
        {
            ChemistryBiochemistry entity;
            public DefaultJsonSerializer(ChemistryBiochemistry entity)
                : base()
            {
                this.entity = entity;
            }

            public ChemistryBiochemistry Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "urea":
                        entity.urea = DbConvert.ToNullableDecimal(propValue) ?? entity.urea;
                        break;
                    case "na":
                        entity.na = DbConvert.ToNullableDecimal(propValue) ?? entity.na;
                        break;
                    case "ka":
                        entity.ka = DbConvert.ToNullableDecimal(propValue) ?? entity.ka;
                        break;
                    case "cl":
                        entity.cl = DbConvert.ToNullableDecimal(propValue) ?? entity.cl;
                        break;
                    case "acid":
                        entity.acid = DbConvert.ToNullableDecimal(propValue) ?? entity.acid;
                        break;
                    case "ca":
                        entity.ca = DbConvert.ToNullableDecimal(propValue) ?? entity.ca;
                        break;
                    case "sugar":
                        entity.sugar = DbConvert.ToNullableDecimal(propValue) ?? entity.sugar;
                        break;
                    case "creatinine":
                        entity.creatinine = DbConvert.ToNullableDecimal(propValue) ?? entity.creatinine;
                        break;
                    case "co2":
                        entity.co2 = DbConvert.ToNullableDecimal(propValue) ?? entity.co2;
                        break;
                    case "uricAcid":
                        entity.uricAcid = DbConvert.ToNullableDecimal(propValue) ?? entity.uricAcid;
                        break;
                    case "citricAcid":
                        entity.citricAcid = DbConvert.ToNullableDecimal(propValue) ?? entity.citricAcid;
                        break;
                    case "phosphorus":
                        entity.phosphorus = DbConvert.ToNullableDecimal(propValue) ?? entity.phosphorus;
                        break;
                    case "albumin":
                        entity.albumin = DbConvert.ToNullableDecimal(propValue) ?? entity.albumin;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //尿素
                writer.WritePropertyName("urea");
                if (entity.Urea == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Urea.Value);
                }

                //钠
                writer.WritePropertyName("na");
                if (entity.Na == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Na.Value);
                }

                //钾
                writer.WritePropertyName("ka");
                if (entity.Ka == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Ka.Value);
                }

                //氯
                writer.WritePropertyName("cl");
                if (entity.Cl == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Cl.Value);
                }

                //草酸
                writer.WritePropertyName("acid");
                if (entity.Acid == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Acid.Value);
                }

                //钙
                writer.WritePropertyName("ca");
                if (entity.Ca == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Ca.Value);
                }

                //葡萄糖
                writer.WritePropertyName("sugar");
                if (entity.Sugar == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Sugar.Value);
                }

                //肌酐
                writer.WritePropertyName("creatinine");
                if (entity.Creatinine == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Creatinine.Value);
                }

                //二氧化碳结合力
                writer.WritePropertyName("co2");
                if (entity.Co2 == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Co2.Value);
                }

                //尿酸
                writer.WritePropertyName("uricAcid");
                if (entity.UricAcid == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.UricAcid.Value);
                }

                //枸橼酸
                writer.WritePropertyName("citricAcid");
                if (entity.CitricAcid == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.CitricAcid.Value);
                }

                //磷
                writer.WritePropertyName("phosphorus");
                if (entity.Phosphorus == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Phosphorus.Value);
                }

                //白蛋白
                writer.WritePropertyName("albumin");
                if (entity.Albumin == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Albumin.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}